
import 'package:extended_tabs/extended_tabs.dart';
import 'package:flutter/material.dart';

import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:yuellyfactory/util/AuthUtil.dart';
import 'package:yuellyfactory/util/ViewUtil.dart';

import '../util/ColorUtil.dart';

class MainTopButton extends StatefulWidget {

  final Function onPressed;

  const MainTopButton({super.key, required this.onPressed});

  @override
  State<MainTopButton> createState() => _MainTopButtonState();
}

class _MainTopButtonState extends State<MainTopButton> with SingleTickerProviderStateMixin  {
  int currentIndex=0;
  final Color activeColor = ColorUtil.colorBlue;
  final Color inactiveColor= Colors.white;
  late TabController _tabController;
  int _previousIndex = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(length: 5, vsync: this);
    // 监听 TabController 变化
    _tabController.addListener(() {
      // 确保索引有效且是新的索引（避免重复触发）
      if (_tabController.indexIsChanging &&
          _tabController.index != _previousIndex) {
        _previousIndex = _tabController.index;
        // 处理标签切换事件
        widget.onPressed(_tabController.index);
      }
    });

  }

  @override
  Widget build(BuildContext context) {
    return   TabBar(
      controller: _tabController,
      // 设置指示器颜色为蓝色
      indicatorColor: Colors.blue,
      labelColor: Colors.blue,
      // 未选中标签的字体颜色
      unselectedLabelColor: Colors.black,
      // 指示器高度
      indicatorWeight: 3.0,
      // 去除底部分割线
      indicatorSize: TabBarIndicatorSize.label,
      // 去除底部分割线
      dividerColor: Colors.transparent,
      padding: EdgeInsets.zero,
      // 标签之间的间距
      labelPadding: const EdgeInsets.symmetric(horizontal:1),
      isScrollable: true,
      // 可选项：设置选中/未选中的字体样式
      labelStyle: const TextStyle(
        fontSize: 16,
        fontWeight: FontWeight.w500,
      ),
      tabs:  [
        AuthUtil.instance.getMenuWidget(AuthUtil.Menu_Base_Manage,
            Container(
          width: 100,
          alignment: Alignment.center,
          child: const Tab(text: '基础数据'),
        )),
        AuthUtil.instance.getMenuWidget(AuthUtil.Menu_Model_Manage,
          Container(
            width: 100,
            alignment: Alignment.center,
            child: const Tab(text: '机型配置'),
          ),
        ),
        AuthUtil.instance.getMenuWidget(AuthUtil.Menu_Purchase_Manage,
          Container(
            width: 100,
            alignment: Alignment.center,
            child: const Tab(text: '成本核算'),
          ),
        ),

        AuthUtil.instance.getMenuWidget(AuthUtil.Menu_Purchase_Manage,
          Container(
            width: 100,
            alignment: Alignment.center,
            child: const Tab(text: '价格选型'),
          ),
        ),
        AuthUtil.instance.getMenuWidget(AuthUtil.Menu_Purchase_Manage,
          Container(
            width: 100,
            alignment: Alignment.center,
            child: const Tab(text: '供应商管理'),
          ),
        ),




      ],
    );
    //   Container(
    //
    //
    //   child: Row(
    //
    //   children: [
    //     itemButton(0, '基础数据'),
    //     SizedBox(width: ViewUtil.screenWidth(10),),
    //     itemButton(1, '机型配置'),
    //     SizedBox(width: ViewUtil.screenWidth(10),),
    //     itemButton(2, '报价系统'),
    //     SizedBox(width: ViewUtil.screenWidth(10),),
    //   ],
    // ),);
  }


  Widget itemButton(int index,var label){
    return ElevatedButton(
      onPressed: () {
        currentIndex=index;
        setState(() {

        });
        widget.onPressed(index);
      },
      style: ElevatedButton.styleFrom(
        backgroundColor: currentIndex==index ? activeColor : inactiveColor,
        foregroundColor: Colors.white,
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
      child: Text(label,style: TextStyle(color: currentIndex==index?Colors.white:ColorUtil.colorBlue),),
    );
  }
}
